﻿
using System;
using System.Collections.Generic;
using QFramework;
using UnityEngine;

public class ExpUpgradeItem
{
    public ExpUpgradeItem(bool isWeapon)
    {
        IsWeapon = isWeapon;
    }
    
    public bool IsWeapon = false;// 是否是武器，只有武器才会在最开始随机一个，炸弹则不会随机，避免默认随机的是炸弹，这样角色就无法造成伤害
    
    public string Key { get; private set; } // key值
    
    public string Name { get; private set; } // 技能名称
    
    //public string Description => mDescriptionFactory(CurrentLevel.Value+1);//描述，用于设置button里text的显示，根据当前等级返回对应的技能描述
    public string Description => mDescDic[CurrentLevel.Value+1];//描述，用于设置button里text的显示，根据当前等级返回对应的技能描述
    
    public int MaxLevel { get; private set; } // 该升级项的最高等级
    
    public string IconName { get; private set; } // 技能图标名称

    public BindableProperty<int> CurrentLevel = new BindableProperty<int>(0); // 该升级项当前等级

    public bool UpgradeFinish { get; set; } = false; // 是否已经升级完成

    public BindableProperty<bool> Visible = new BindableProperty<bool>(); // 是否显示隐藏

    private Func<int, string> mDescriptionFactory;
    
    private Dictionary<int, string> mDescDic = new Dictionary<int, string>();//储存技能在不同等级的描述信息
    
    /// <summary>
    /// 升级事件，会提升等级并触发升级事件
    /// </summary>
    public void Upgrade()
    {
        Debug.Log($"随机到技能Id:{Key},技能名称：{Name}");
        CurrentLevel.Value++;//当前等级+1
        mOnUpgrade?.Invoke(this,CurrentLevel.Value);
        if (CurrentLevel.Value>=MaxLevel)
        {
            UpgradeFinish = true;
        }
        //ExpUpgradeSystem.OnCoinUpgradeSystemChanged.Trigger();
    }

    private Action<ExpUpgradeItem,int> mOnUpgrade;//用<>的目的是在调用方法的时候可以把当前ExpUpgradeItem返回出去，可以在外面调用

    private Func<ExpUpgradeItem, bool> mCondition;//当前的状态，是否已经升级过
    
    /// <summary>
    /// 设置Key
    /// </summary>
    /// <param name="key"></param>
    /// <returns></returns>
    public ExpUpgradeItem Withkey(string key)
    {
        Key = key;
        return this;
    }
    
    /// <summary>
    /// 设置技能名称
    /// </summary>
    /// <param name="name"></param>
    /// <returns></returns>
    public ExpUpgradeItem WithName(string name)
    {
        Name = name;
        return this;
    }

    /// <summary>
    /// 设置描述
    /// </summary>
    /// <param name="descriptionFactory"></param>
    /// <returns></returns>
    public ExpUpgradeItem WithDescription(Func<int,string> descriptionFactory)
    {
        mDescriptionFactory = descriptionFactory;
        return this;
    }

    public ExpUpgradeItem WithDescription(Dictionary<int, string> descDic)
    {
        mDescDic = descDic;
        return this;
    }
    
    // 配对后技能名称、描述、图标等信息（也就是升级后的超级武器）
    public string PairedName { get; private set; }
    public string PairedDescription { get; private set; }
    public string PairedIconName { get; private set; }

    public ExpUpgradeItem WithPairedName(string pairedName)
    {
        PairedName = pairedName;
        return this;
    }
    
    public ExpUpgradeItem WithPairedDescription(string pairedDescription)
    {
        PairedDescription = pairedDescription;
        return this;
    }
    
    public ExpUpgradeItem WithPairedIconName(string pairedIconName)
    {
        PairedIconName = pairedIconName;
        return this;
    }

    /// <summary>
    /// 设置icon名称
    /// </summary>
    /// <param name="iconName"></param>
    /// <returns></returns>
    public ExpUpgradeItem WithIconName(string iconName)
    {
        IconName = iconName;
        return this;
    }

    /// <summary>
    /// 设置升级事件
    /// </summary>
    /// <param name="onUpgrade"></param>
    /// <returns></returns>
    public ExpUpgradeItem OnUpgrade(Action<ExpUpgradeItem,int> onUpgrade)
    {
        mOnUpgrade = onUpgrade;
        return this;
    }

    /// <summary>
    /// 设置当前状态，是否已经升级过
    /// </summary>
    /// <param name="condition"></param>
    /// <returns></returns>
    public ExpUpgradeItem Condition(Func<ExpUpgradeItem, bool> condition)
    {
        mCondition = condition;
        return this;
    }

    /// <summary>
    /// 设置最大等级
    /// </summary>
    /// <param name="maxLevel"></param>
    /// <returns></returns>
    public ExpUpgradeItem WithMaxLevel(int maxLevel)
    {
        MaxLevel = maxLevel;
        return this;
    }
}
